<?php
	
	namespace Aspic;
	
	/**
	* Aspic base class. It provide method to access Globals arrays and usefull functions
	* It is made to be used by class (inheritance) that need global control
	* 
	* @uses Util
	* 
	* @Author Mallory Dessaintes <mdessaintes@gmail.com>
	* @version 1.0
	*/
	abstract class Base {
		
		protected static $_predefinedHeaders = array(
			'json' => array(
				'name' => 'Content-type', 
				'value' => 'application/json'
			), 
			'css' => array(
				'name' => 'Content-type', 
				'value' => 'text/css'
			), 
			'js' => array(
				'name' => 'Content-type', 
				'value' => 'application/javascript'
			), 
			'utf8' => array(
				'name' => 'Content-type', 
				'value' => 'text/html; charset=utf8'
			), 
		);
		
		/**
		* Add a header
		*/
		public static function header($hName, $hValue) {
			header(sprintf('%s: %s', $hName, $hValue));
		}
		
		/**
		* Add a predifined header
		*/
		public static function pHeader($hName) {
			if(isset(static::$_predefinedHeaders[$hName])) {
				$header = static::$_predefinedHeaders[$hName];
				
				header(sprintf('%s: %s', $header['name'], $header['value']));
			}
			else {
				throw new Exception('Predefined header "'.$hName.'" does not exist');
			}
		}
	
		/**
		* Set a location header that will redirect the user to a given url
		*/
		public static function redirect($url) {
			static::header('location',$url);
		}
		
		public static function getPostData($name, $convertType = null) {
			$value = Util::getIfIsSet($_POST[$name], $convertType);
			
			return $value;
		}
		
		public static function getGetData($name, $convertType = null) {
			$value = Util::getIfIsSet($_GET[$name], $convertType);
			
			return $value;
		}
		
		public static function getRequestData($name, $convertType = null) {
			$value = Util::getIfIsSet($_REQUEST[$name], $convertType);
			
			return $value;
		}
		
		public static function getFilesData($name) {
			return Util::getIfIsSet($_FILES[$name]);
		}
		
		public static function getEnvData($name) {
			return Util::getIfIsSet($_ENV[$name]);
		}
		
		public static function getServerData($name) {
			return Util::getIfIsSet($_SERVER[$name]);
		}
		
	}
	
?>